import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'


Vue.use(VueRouter)


const routes = [
  {
    path: '/',
    redirect: '/home/index',

  },
  {

    path: '/about',
    name: 'about',

    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },

  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children: [
      {
        path: 'index',
        name: '/home/index',
        component: () => import('../views/Index.vue'),
        meta: {//动态生成导航
          nav: ['首页']
        }
      },
      {
        path: 'com-list',
        name: '/home/com-list',
        component: () => import('../views/commodity/commodityList.vue'),
        meta: {//动态生成导航
          nav: ['']
        }
      },
      {//修改商品
        path: 'up-com/:id',
        name: '/home/up-com',
        component: () => import('../views/commodity/upCom.vue'),
        meta: {//动态生成导航
          nav: ['']
        }
      },
      {
        path: 'com-add',
        name: '/home/com-add',
        component: () => import('../views/commodity/commodityAdd.vue'),
        meta: {//动态生成导航
          nav: ['']
        }
      }, {//我的订单
        path: 'order-list',
        name: '/home/order-list',
        component: () => import('../views/order/orderList.vue'),
        meta: {
          nav: ['']
        }
      }, {//已取消
        path: 'order-cancel',
        name: '/home/order-cancel',
        component: () => import('../views/order/orderCancel.vue'),
        meta: {
          nav: ['']
        }
      }, {//退货/退款
        path: 'order-refund',
        name: '/home/order-refund',
        component: () => import('../views/order/orderRefund.vue'),
        meta: {
          nav: ['']
        }
      }, {//用户列表
        path: 'user-list',
        name: '/user-list',
        component: () => import('../views/user/User.vue'),
        meta: {
          nav: ['']
        },
      },
      {//门店管理 门店列表
        path: 'shop',
        name: '/shop',
        component: () => import('../views/shop/shop.vue'),
        meta: {
          nav: ['']
        }
      },
      {//门店管理 添加门店
        path: 'shop-add',
        name: '/shop-add',
        component: () => import('../views/shop/shopAdd.vue'),
        meta: {
          nav: ['']
        }
      },
      , {//我的物流
        path: 'my-log',
        name: '/my-log',
        component: () => import('../views/logistics/myLog.vue'),
        meta: {
          nav: ['']
        }
      },
      , {//设置物流
        path: 'set-log',
        name: '/set-log',
        component: () => import('../views/logistics/setLog.vue'),
        meta: {
          nav: ['']
        }
      },
    ]

  },
  {
    path: '/user/login',
    name: '/user/login',
    component: () => import('../views/user/Login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes


})

/** 全局前置导航守卫 每当路由跳转到目标组件之前会自动执行箭头函数 */
import store from '../store'
router.beforeEach((to, from, next) => {
  // console.log('from', from)
  // console.log('to', to)
  // 判断目标地址是否是/user/login
  if (to.path == '/user/login') {
    next()  //如果希望继续正常的跳转流程，则调用next()即可
  } else {
    // 如果不是跳转到登录页面，则需要验证vuex中是否有登录用户
    if (store.state.user) {
      // 如果已经登录，可以跳转
      next()
    } else { // 如果没有登录，则强制跳转到登录页
      router.push('/user/login')
    }
  }
})




export default router
